package com.example.demo.test;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class S3ResourceUrlHelper {

//    private static final Pattern pattern = Pattern.compile(".*(upload/vod/user([0-9]+)/.*)");
//    private static final Pattern pattern2 = Pattern.compile(".*(userUpload/([0-9]+)/.*)");
    private static final Pattern pattern = Pattern.compile(".*(upload/vod/user([0-9]+)/.*.(mp3|mp4|m3u8|webm))");
    private static final Pattern pattern2 = Pattern.compile(".*(userUpload/([0-9]+)/.*.(png|gif|jpeg|jpg|bmp|raw|psd))");
    private static final Pattern pattern3 = Pattern.compile(".*(upload/vod/user([0-9]+)/.*)");
    private static final Pattern pattern4 = Pattern.compile(".*(userUpload/([0-9]+)/.*)");
    private static final Logger log = LoggerFactory.getLogger(S3ResourceUrlHelper.class);

    public static String getS3Url(String url) {
        if (isEmptyString(url)) {
            return null;
        }
        String resouceUrl = getMatchKeyUrl(url);
//        String generatePresignedUrl = GeneratePresignedUrl.getS3Url(resouceUrl);
//        resouceUrl = getMatchKeyUrl2(generatePresignedUrl);
        return resouceUrl;
    }

    public static String getAllS3Url(String url) {
        if (isEmptyString(url)) {
            return null;
        }
        String resouceUrl = getMatchKeyUrl(url);
        resouceUrl = GeneratePresignedUrl.getS3Url(resouceUrl);
        return resouceUrl;
    }

    public static String getNoS3Url(String url) {
        if (isEmptyString(url)) {
            return null;
        }
        String resouceUrl = getMatchKeyUrl(url);
        return resouceUrl;
    }

    private static String getMatchKeyUrl2(String url) {
        if (isEmptyString(url)) {
            return null;
        }
        Matcher matcher = pattern3.matcher(url);
        if (matcher.find()) {
            try {
                return matcher.group(1);
            } catch (Exception e) {
                return null;
            }
        }
        Matcher matcher2 = pattern4.matcher(url);
        if (matcher2.find()) {
            try {
                return matcher2.group(1);
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }

    public static String getMatchKeyUrl(String url) {
        if (isEmptyString(url)) {
            return null;
        }
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            try {
                return matcher.group(1);
            } catch (Exception e) {
                return null;
            }
        }
        Matcher matcher2 = pattern2.matcher(url);
        if (matcher2.find()) {
            try {
                return matcher2.group(1);
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }

    public static String wrapperVideoUrl(String vodUrl) {

        return "";
    }
    public static String wrapperAudioUrl(String vodUrl) {

        return "";
    }
    public static String wrapperImageUrl(String vodUrl) {
        return "";
    }

    // 判断是否是空字符串
    private static boolean isEmptyString(final String str)
    {
        return null == str || str.isEmpty() || "".equals(str);

    }

    public static String decryptWithPKCS5PaddingHex(String content, String ekey) {
        try {
            byte[] raw = ekey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);// 初始化
            byte[] result = cipher.doFinal(parseHexStr2Byte(content));
            return new String(result); // 加密
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
    public static void main(String[] args) {
        String filePath = "/path/to/your/file.png";
        String substring = filePath.substring(filePath.lastIndexOf("."));
        System.out.println(substring);
        String pwd = decryptWithPKCS5PaddingHex("ebd18ef58aff190e8e27a9c435bc6caf", "9ncEYCpm+5XQpg==");
        System.out.println(pwd);

//        String url = "https://video.people.cn/userUpload/1735982106105323/1735982106105323/picture/ad09d3bd-d7b1-4a4a-8e45-f8f54b996e83.jpeg";
//        String s3Url = getS3Url(url);
//        System.out.println(s3Url);

//        Map<String,String> map = new HashMap<>();
//        String json = "[\"name\",\"John\"]";
//        map.put("key",json);

//        List<String> list = new ArrayList<>();
//        list.add("name");
//        list.add("John");
//        String jsonString = JSON.toJSONString(list);
//        map.put("key",jsonString);
//        json1(map);
//        json = "[9999,4444]";
       // json = "[]";
//        json = "aaaa,bb";
//        map.put("key",json);
//        JSONObject jsonObject  = new JSONObject();
//        jsonObject.put("key","val");
//        System.out.println(jsonObject);
//        String jsonString = jsonObject.toJSONString(jsonObject);
//        System.out.println(jsonString);
//        String aa = "{"key":"cal"}”;

//        JSONArray jsonArray = JSON.parseArray(json);
//        System.out.println("验证成功");
////        if (jsonArray != null && jsonArray.size() == 0) {
////            System.out.println("不能为空[]！");
////
////        }
//        if (jsonArray.size() == 0) {
//            System.out.println("不能为空[]！");
//        }
//        for (Object o : jsonArray) {
//            if (!(o instanceof String)) {
//                System.out.println("格式错误！");
//            }
//        }
//
////        System.out.println(JsonUtil.toJson(map));
//        System.out.println(JSON.toJSONString(map));


    }

    private static void json1(Map<String, String> map) {
        System.out.println(map);
        System.out.println(JsonUtil.toJson(map));

        System.out.println(JSON.toJSONString(map));
    }
}
